package com.hzit.test;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.baomidou.mybatisplus.generator.fill.Column;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;


public class MyBatisPlusGeneratorDemo {
	static String url = "jdbc:mysql://127.0.0.1:3306/bj2307?useSSL=false&serverTimeZone=Asia/Shanghai";
	static DataSourceConfig.Builder DATA_SOURCE_CONFIG = new DataSourceConfig.Builder(url,"root","1234");


	public static void main(String[] args) {
		FastAutoGenerator.create(DATA_SOURCE_CONFIG)
				// 全局配置
				.globalConfig((scanner, builder) -> builder.author(scanner.apply("请输入作者名称？"))
						.fileOverride()
						.outputDir("d:/mp")         // 设置输出目录
				)
				// 包配置
				.packageConfig((scanner, builder) -> builder.parent(scanner.apply("请输入包名？")))
				// 策略配置
				.strategyConfig((scanner, builder) ->
						builder.addInclude(getTables(scanner.apply("请输入表名，多个英文逗号分隔？所有输入 all")))
								.addTablePrefix("tb_","sys_")                  // 设置表名的前缀，生成的实体类会自动去掉表名前缀
								//.addInclude("student","classes")        // 所包含的表
						.controllerBuilder().enableRestStyle().enableHyphenStyle()
						.entityBuilder().enableLombok().addTableFills(
								new Column("create_time", FieldFill.INSERT)
						).build())
				.templateEngine(new FreemarkerTemplateEngine())
				/*
					模板引擎配置，默认 Velocity 可选模板引擎 Beetl 或 Freemarker
				   .templateEngine(new BeetlTemplateEngine())
				   .templateEngine(new FreemarkerTemplateEngine())
				 */
				.execute();
	}



	// 处理 all 情况
	protected static List<String> getTables(String tables) {
		return "all".equals(tables) ? Collections.emptyList() : Arrays.asList(tables.split(","));
	}
}
